//
//  BCCouponViewController.m
//  BangcaiBuy
//
//  Created by lsy on 16/6/13.
//  Copyright © 2016年 BCB. All rights reserved.
//

#import "BCCouponViewController.h"
#import "BCCouponCollectionViewCell.h"

@interface BCCouponViewController () <UICollectionViewDelegate,UICollectionViewDataSource>
//种类选择视图
@property (nonatomic, strong) UIView *segmentView;
//种类选择按钮背景
@property (nonatomic, strong) UIView *segmentItem_0_BackgroundView;
@property (nonatomic, strong) UIView *segmentItem_1_BackgroundView;
//种类选择按钮
@property (nonatomic, strong) UIButton *segmentItem_0;
@property (nonatomic, strong) UIButton *segmentItem_1;
//主视图
@property (nonatomic, strong) UICollectionView *mainCollectionView;
@end

@implementation BCCouponViewController
-(void)viewDidLoad {
    [super viewDidLoad];
    [self p_initNavgationBar];
//    [self p_initSegmentView];
    [self p_initCollectionView];
}

-(void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    //隐藏导航栏
    self.navigationController.navigationBar.hidden = NO;

    if (!self.rdv_tabBarController.tabBarHidden) {
        [[self rdv_tabBarController] setTabBarHidden:YES animated:YES];
    }
}

-(void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    if (self.rdv_tabBarController.tabBarHidden) {
        [[self rdv_tabBarController] setTabBarHidden:NO animated:YES];
    }
}

#pragma mark - View init
-(void)p_initNavgationBar {
    self.title = @"优惠券";
    self.view.backgroundColor = [UIColor colorWithRed:242.f/255.f green:242.f/255.f blue:242.f/255.f alpha:1];
}

//- (void)p_initSegmentView {
//    self.segmentView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 44.f)];
//    self.segmentView.backgroundColor = [UIColor colorWithRed:242.f/255.f green:242.f/255.f blue:242.f/255.f alpha:1];
//    [self.view addSubview:self.segmentView];
//    self.segmentItem_0_BackgroundView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.segmentView.frame.size.width/2, self.segmentView.frame.size.height)];
//    self.segmentItem_0_BackgroundView.backgroundColor = BangcaiGreen;
//    self.segmentItem_0_BackgroundView.hidden = NO;
//    self.segmentItem_0 = [UIButton buttonWithType:UIButtonTypeCustom];
//    self.segmentItem_0.frame = CGRectMake(0, 0, self.segmentItem_0_BackgroundView.frame.size.width, self.segmentItem_0_BackgroundView.frame.size.height-3.f);
//    [self.segmentItem_0 setTitle:@"可用优惠券" forState:UIControlStateNormal];
//    [self.segmentItem_0 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
//    [self.segmentItem_0 setBackgroundColor:[UIColor colorWithRed:242.f/255.f green:242.f/255.f blue:242.f/255.f alpha:1]];
//    self.segmentItem_0.tag = 0;
//    [self.segmentItem_0 addTarget:self action:@selector(segmentAction:) forControlEvents:UIControlEventTouchUpInside];
//    [self.segmentView addSubview:self.segmentItem_0_BackgroundView];
//    [self.segmentView addSubview:self.segmentItem_0];
//    self.segmentItem_1_BackgroundView = [[UIView alloc] initWithFrame:CGRectMake(self.segmentItem_0_BackgroundView.frame.size.width, 0, self.segmentView.frame.size.width/2, self.segmentView.frame.size.height)];
//    self.segmentItem_1_BackgroundView.backgroundColor = BangcaiGreen;
//    self.segmentItem_1_BackgroundView.hidden = YES;
//    self.segmentItem_1 = [UIButton buttonWithType:UIButtonTypeCustom];
//    self.segmentItem_1.frame = CGRectMake(self.segmentItem_0_BackgroundView.frame.size.width, 0, self.segmentItem_1_BackgroundView.frame.size.width, self.segmentItem_1_BackgroundView.frame.size.height-3.f);
//    [self.segmentItem_1 setTitle:@"已过期" forState:UIControlStateNormal];
//    [self.segmentItem_1 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
//    [self.segmentItem_1 setBackgroundColor:[UIColor colorWithRed:242.f/255.f green:242.f/255.f blue:242.f/255.f alpha:1]];
//    self.segmentItem_1.tag = 1;
//    [self.segmentItem_1 addTarget:self action:@selector(segmentAction:) forControlEvents:UIControlEventTouchUpInside];
//    [self.segmentView addSubview:self.segmentItem_1_BackgroundView];
//    [self.segmentView addSubview:self.segmentItem_1];
//}

- (void)p_initCollectionView {
    UICollectionViewFlowLayout *flowLayout= [[UICollectionViewFlowLayout alloc]init];
    self.mainCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) collectionViewLayout:flowLayout];
    self.mainCollectionView.backgroundColor = [UIColor whiteColor];
    self.mainCollectionView.delegate = self;
    self.mainCollectionView.dataSource = self;
    //设置contentInset属性（上左下右的值）
    self.mainCollectionView.contentInset = UIEdgeInsetsMake(0, 0, 0, 0);
    [self.view addSubview:self.mainCollectionView];
}

#pragma mark - Private Action
//- (void)segmentAction:(UIButton *)item {
//    if (0 == item.tag) {
//        self.segmentItem_0_BackgroundView.hidden = NO;
//        self.segmentItem_1_BackgroundView.hidden = YES;
//    }
//    else {
//        self.segmentItem_0_BackgroundView.hidden = YES;
//        self.segmentItem_1_BackgroundView.hidden = NO;
//    }
//}

#pragma mark - UICollectionViewDataSource
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return 3;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *ID = @"CouponCell";
    BCCouponCollectionViewCell *cell = [BCCouponCollectionViewCell cellWithCollectionView:collectionView];
    cell = [collectionView dequeueReusableCellWithReuseIdentifier:ID forIndexPath:indexPath];
    return cell;
}

-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section

{
    UIEdgeInsets top = {0,0,0,0};
    return top;
}

//定义每个UICollectionViewCell 的大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    return CGSizeMake(self.view.frame.size.width, 120);
}

//每个item之间的间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
    return 0;
}

//每个section中不同的行之间的行间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
    
    return 0;
}

//collectionview点击
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    BCCouponCollectionViewCell *cell = (BCCouponCollectionViewCell *)[collectionView cellForItemAtIndexPath:indexPath];
    cell.selectedImageView.hidden = !cell.selectedImageView.hidden;
}

@end
